﻿using ProductMonitor.Model;
using ProductMonitor.UserControls;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace ProductMonitor.ViewModel
{
    public class MainWindowVM : ObservableObject
    {
        // 在构造函数中初始化环境监控数据(以后这些数据要从设备中获取)
        public MainWindowVM()
        {
            #region 初始化环境监控数据
            // 初始化环境监控数据
            // EnviromentList = new List<EnviromentModel>();
            EnviromentList.Add(new EnviromentModel { EnItemName = "光照(Lux)", EnItemValue = 123 });
            EnviromentList.Add(new EnviromentModel { EnItemName = "噪音(db)", EnItemValue = 55 });
            EnviromentList.Add(new EnviromentModel { EnItemName = "温度(℃)", EnItemValue = 80 });
            EnviromentList.Add(new EnviromentModel { EnItemName = "湿度(%)", EnItemValue = 43 });
            EnviromentList.Add(new EnviromentModel { EnItemName = "PM2.5(m³)", EnItemValue = 20 });
            EnviromentList.Add(new EnviromentModel { EnItemName = "硫化氢(PPM)", EnItemValue = 15 });
            EnviromentList.Add(new EnviromentModel { EnItemName = "氮气(PPM)", EnItemValue = 18 });
            #endregion

            // 从设备中读取数据（上位机Modbus）
            Task.Run(() =>
            {
                while (true)
                {
                    using (SerialPort serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One))
                    {
                        // 打开端口号
                        serialPort.Open();
                        // 启用Modbus4
                        Modbus.Device.IModbusSerialMaster master = Modbus.Device.ModbusSerialMaster.CreateRtu(serialPort);
                        // 从设备地址（设备编号），寄存器起始地址，寄存器个数
                        // 功能码03
                        ushort[] value = master.ReadHoldingRegisters(1, 0, 7);
                        // 循环赋值
                        for (int i = 0; i < 7; i++)
                        {
                            EnviromentList[i].EnItemValue = value[i];
                        }

                    }
                }
            });

            #region 初始化报警数据
            // 初始化报警数据
            AlarmList = new List<AlarmModel>();
            AlarmList.Add(new AlarmModel { Num = "01", Msg = "设备温度过高", Time = "2023-11-23 18:34:56", Duration = 7 });
            AlarmList.Add(new AlarmModel { Num = "02", Msg = "车间温度过高", Time = "2023-12-08 20:40:59", Duration = 10 });
            AlarmList.Add(new AlarmModel { Num = "03", Msg = "设备转速过快", Time = "2024-01-05 12:24:34", Duration = 12 });
            AlarmList.Add(new AlarmModel { Num = "04", Msg = "设备气压偏低", Time = "2024-02-04 19:58:00", Duration = 90 });
            #endregion

            #region 初始化设备数据
            DeviceList = new List<DeviceModel>();
            DeviceList.Add(new DeviceModel { DeviceItem = "电能(Kw.h)", Value = 60.8 });
            DeviceList.Add(new DeviceModel { DeviceItem = "电压(V)", Value = 390 });
            DeviceList.Add(new DeviceModel { DeviceItem = "电流(A)", Value = 5 });
            DeviceList.Add(new DeviceModel { DeviceItem = "压差(kpa)", Value = 13 });
            DeviceList.Add(new DeviceModel { DeviceItem = "温度(℃)", Value = 36 });
            DeviceList.Add(new DeviceModel { DeviceItem = "振动(mm/s)", Value = 4.1 });
            DeviceList.Add(new DeviceModel { DeviceItem = "转速(r/min)", Value = 2600 });
            DeviceList.Add(new DeviceModel { DeviceItem = "气压(kpa)", Value = 0.5 });
            #endregion

            #region 初始化雷达数据
            RaderList = new List<RaderModel>();
            RaderList.Add(new RaderModel { ItemName = "排烟风机", Value = 90 });
            RaderList.Add(new RaderModel { ItemName = "客梯", Value = 30.00 });
            RaderList.Add(new RaderModel { ItemName = "供水机", Value = 34.89 });
            RaderList.Add(new RaderModel { ItemName = "喷淋水泵", Value = 69.59 });
            RaderList.Add(new RaderModel { ItemName = "稳压设备", Value = 20 });
            #endregion

            #region 初始化人员缺岗信息
            StuffOutWorkList = new List<StuffOutWorkModel>();
            StuffOutWorkList.Add(new StuffOutWorkModel { StuffName = "张晓婷", Position = "技术员", OutWorkCount = 123 });
            StuffOutWorkList.Add(new StuffOutWorkModel { StuffName = "李晓", Position = "操作员", OutWorkCount = 23 });
            StuffOutWorkList.Add(new StuffOutWorkModel { StuffName = "王克俭", Position = "技术员", OutWorkCount = 134 });
            StuffOutWorkList.Add(new StuffOutWorkModel { StuffName = "陈家栋", Position = "统计员", OutWorkCount = 143 });
            StuffOutWorkList.Add(new StuffOutWorkModel { StuffName = "杨过", Position = "技术员", OutWorkCount = 12 });

            #endregion

            #region 初始化车间列表 
            WorkShopList = new List<WorkShopModel>();
            WorkShopList.Add(new WorkShopModel { WorkShopName = "贴片车间", WorkingCount = 32, WaitCount = 8, WrongCount = 4, StopCount = 0 });
            WorkShopList.Add(new WorkShopModel { WorkShopName = "封装车间", WorkingCount = 20, WaitCount = 8, WrongCount = 4, StopCount = 0 });
            WorkShopList.Add(new WorkShopModel { WorkShopName = "焊接车间", WorkingCount = 68, WaitCount = 8, WrongCount = 4, StopCount = 0 });
            WorkShopList.Add(new WorkShopModel { WorkShopName = "贴片车间", WorkingCount = 32, WaitCount = 10, WrongCount = 4, StopCount = 10 });

            #endregion

            #region 初始化机台列表
            MachineList = new List<MachineModel>();
            Random random = new Random();
            for (int i = 0; i < 20; i++)
            {
                int plan = random.Next(100, 1000);//计划量 随机数
                int finished = random.Next(0, plan);//已完成量
                MachineList.Add(new MachineModel
                {
                    MachineName = "焊接机-" + (i + 1),
                    FinishedCount = finished,
                    PlanCount = plan,
                    Status = "作业中",
                    OrderNo = "H202212345678"
                });
            }
            #endregion

        }

        // 用户控件上的绑定数据还是在主界面的视图模型上去写，因为用户控件就在主界面上
        private UserControl _MonitorUC = new MonitorUC();

		// 监控用户控件
		public UserControl MonitorUC
        {
			get { return _MonitorUC; }
			set { _MonitorUC = value; RaisePropertyChanged(); }
		}


        #region 时间 日期
        //public string TimeStr
        //{
        //	get { return DateTime.Now.ToString("HH:mm"); }
        //}

        // 后来时间肯定是动态变化的，所以老老实实写完整
        private string _TimeStr = DateTime.Now.ToString("HH:mm");

		public string TimeStr
        {
			get { return _TimeStr; }
			set { _TimeStr = value; RaisePropertyChanged(); }
		}

		// 日期
        private string _DateStr = DateTime.Now.ToString("yyyy-MM-dd");

        public string DateStr
        {
            get { return _DateStr; }
            set { _DateStr = value; RaisePropertyChanged(); }
        }

		// 星期
		private string _WeekStr;
        
        public string WeekStr
        {
			get
			{
                int index = (int)DateTime.Now.DayOfWeek;
				string[] week = new string[7] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
				_WeekStr = week[index];
                return _WeekStr;
            }
			set { _WeekStr = value; RaisePropertyChanged(); }
		}
        #endregion

        #region 计数
        // 机器总数
        private string _MachineCount = "0298";

		public string MachineCount
        {
			get { return _MachineCount; }
			set { _MachineCount = value; RaisePropertyChanged(); }
		}

        // 生产总数
        private string _ProductCount = "1643";

        public string ProductCount
        {
            get { return _ProductCount; }
            set { _ProductCount = value; RaisePropertyChanged(); }
        }

        // 不良计数
        private string _BadCount = "0403";

        public string BadCount
        {
            get { return _BadCount; }
            set { _BadCount = value; RaisePropertyChanged(); }
        }

        #endregion

        #region 环境监控数据

        private List<EnviromentModel> _EnviromentList = new List<EnviromentModel>();

        public List<EnviromentModel> EnviromentList
        {
            get { return _EnviromentList; }
            set { _EnviromentList = value; RaisePropertyChanged(); }
        }
        #endregion

        #region 报警属性

        private List<AlarmModel> _AlarmList;

        public List<AlarmModel> AlarmList
        {
            get { return _AlarmList; }
            set { _AlarmList = value; RaisePropertyChanged(); }
        }
        #endregion

        #region 设备数据
        private List<DeviceModel> _DeviceList;

        public List<DeviceModel> DeviceList
        {
            get { return _DeviceList; }
            set { _DeviceList = value; RaisePropertyChanged(); }
        }
        #endregion

        #region 雷达数据
        private List<RaderModel> _RaderList;

        public List<RaderModel> RaderList
        {
            get { return _RaderList; }
            set { _RaderList = value; RaisePropertyChanged(); }
        }
        #endregion

        #region 缺岗人员
        private List<StuffOutWorkModel> _StuffOutWorkList;

        public List<StuffOutWorkModel> StuffOutWorkList
        {
            get { return _StuffOutWorkList; }
            set { _StuffOutWorkList = value; RaisePropertyChanged(); }
        }
        #endregion

        #region 车间数量
        private List<WorkShopModel> _WorkShopList;

        public List<WorkShopModel> WorkShopList
        {
            get { return _WorkShopList; }
            set { _WorkShopList = value; RaisePropertyChanged(); }
        }
        #endregion

        #region 机台数量
        private List<MachineModel> _MachineList;

        public List<MachineModel> MachineList
        {
            get { return _MachineList; }
            set { _MachineList = value; RaisePropertyChanged(); }
        }
        #endregion

    }
}
